<template>
  <div class="page-container">
    <SearchForm
      :search-list="searchList"
      :search-params="searchParams"
      @search="onSearch"
      @reset="onReset"
    />
    <el-form>
      <el-form-item>
        <div style="text-align: right">
          <el-button type="primary" @click="onAdd">新增</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table
        v-loading="loading"
        height="100%"
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column prop="ship.name" label="出海项目" />
        <el-table-column prop="start_time" label="出海时间" />
        <el-table-column prop="end_time" label="返回时间" />
        <el-table-column prop="duration" label="时长" width="80px"/>
        <el-table-column label="乘客人数" width="120px">
          <template slot-scope="scope">
            <el-button type="text">成人：{{ scope.row.orderList.filter(item => item.status === 1).reduce((pre, next) => pre + next.adult_count, 0) }}</el-button>
            </br>
            <el-button type="text" style="margin-left: 0">儿童：{{ scope.row.orderList.filter(item => item.status === 1).reduce((pre, next) => pre + next.child_count, 0) }} </el-button>
            </br>
            <el-button type="text" style="margin-left: 0">6岁以下：{{ scope.row.orderList.filter(item => item.status === 1).reduce((pre, next) => pre + next.childs_count, 0) }} </el-button>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope" v-if="scope.row.ship.code === 'Custom'">
            <el-tag type="danger" v-if="scope.row.orderList[0].status === 0">待支付</el-tag>
            <el-tag type="success" v-if="scope.row.orderList[0].status === 1">已支付</el-tag>
            <el-tag type="info" v-if="scope.row.orderList[0].status === -1">已取消</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100px">
          <template slot-scope="scope">
            <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
            <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-box">
      <el-pagination
        background
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        @size-change="changePageSize"
        @current-change="changePageNo"
      />
    </div>
    <Add ref="addFormRef" @get-page="initData" />
    <Edit ref="editFormRef" @get-page="initData" />
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import { http } from '@/api'
import Add from './add.vue'
import Edit from './edit.vue'
export default {
  components: {
    Add,
    Edit
  },
  mixins: [crud],
  data() {
    return {
      request: http.cycle,
      searchList: [
        {
          key: 'start_time',
          name: 'datetime',
          label: '开始时间',
          dateType: 'date'
        },
        {
          key: 'end_time',
          name: 'datetime',
          label: '结束时间',
          dateType: 'date'
        },
        {
          key: 'ship_id',
          name: 'select',
          label: '出海项目',
          options: [
          ]
        },
      ],
      searchParams: {
        start_time: '',
        end_time: '',
        ship_id: ''
      },
      columns: [
        { prop: 'shipName', label: '行程' },
        { prop: 'start_time', label: '出发时间' },
        { prop: 'count', label: '人数' },
        // { prop: 'status', label: '状态', type: 'select', mapping: { 1: '启用', 0: '禁用' } },
        { prop: 'create_time', label: '创建时间' }
      ],
      tableData: [
      ]
    }
  },
  created() {
    http.ship.list().then(res => {
      this.searchList[2].options = res.data.map(el => ({ name: el.name, value: el.id }))
    })
  },
  methods: {
    formatTableData(data) {
      data.forEach(el => {
        if (el.ship) {
          el.shipName = el.ship.name
        }
      })
    },
    onAdd() {
      this.$refs.addFormRef.open()
    },
    onEdit(row) {
      this.$refs.editFormRef.open(row)
    }
  }
}
</script>

<style scoped lang="scss">
</style>
